package com.ruoyi.wisdom.event;

import com.ruoyi.wisdom.domain.IotSchoolGrade;
import com.ruoyi.wisdom.domain.swap.SwapSchoolGrade;
import com.ruoyi.wisdom.service.IIotSchoolGradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class SchoolGradeEvent extends IotEvent{

    @Autowired
    private IIotSchoolGradeService iotSchoolGradeService;

    /**
     * 处理
     * @param swapSchoolGrade
     */
    public void handle(SwapSchoolGrade swapSchoolGrade) {
        if (swapSchoolGrade.getOperate() == null) {
            save(swapSchoolGrade);
        } else {
            switch (swapSchoolGrade.getOperate()) {
                case "delete":
                case "remove":
                    delete(swapSchoolGrade);
                    break;
                default:
                    save(swapSchoolGrade);
            }
        }
    }

    /**
     * 添加|编辑
     * @param swapSchoolGrade
     */
    public void save(SwapSchoolGrade swapSchoolGrade)
    {
        IotSchoolGrade result = iotSchoolGradeService.selectIotSchoolGradeById(swapSchoolGrade.getId());
        // 实体装换
        IotSchoolGrade iotSchoolClasses = getModelMapper().map(swapSchoolGrade, IotSchoolGrade.class);
        if (result != null) {
            iotSchoolClasses.setId(swapSchoolGrade.getId());
            iotSchoolGradeService.updateIotSchoolGrade(iotSchoolClasses);
        } else {
            iotSchoolGradeService.insertIotSchoolGrade(iotSchoolClasses);
        }
    }

    /**
     * 删除
     * @param swapSchoolGrade
     */
    public void delete(SwapSchoolGrade swapSchoolGrade){
        iotSchoolGradeService.deleteIotSchoolGradeById(swapSchoolGrade.getId());
    }
}
